/**!
 * @file    sensor_demo.c
 * @brief   传感器数据实时上传demo程序
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "defines.h"
#include "merrno.h"
#include "Serial.h"
#include "string_parser.h"
#include "ezxml/ezxml.h"

static int parse_arguments(int argc, char **argv)
{
    int i = 0;

    while (i < argc) {
        char *t = argv[i];
        if (strcmp(t, "-c") == 0) {
            i++;
            if (i < argc) {
//                g_cfgFileName = argv[i];
            }
        } else if (strcmp(t, "-f") == 0) {
            i++;
            if (i < argc) {
//                g_logFile = atoi(argv[i]);
//                log_info("parse_arguments: change log file key %d", g_logFile);
            }
        }
        i++;
    }   

    return TRUE;
}

/**!
 * @brief   main函数参数处理
 */
#define CTD_STRING1 "21.5571,  0.10623,   -9.199,  6.026,   0.5660, 1487.537,  0.11409, 07 Jun 2018, 13:58:10, 2"
#define CTD_STRING2 "524276,                 2886.656,                   785035,    2706,   20 Aug 2012, 09:01:34"
char port[24] = "/dev/ttyUSB0";
int baud = 115200;
static int fd_serial;
#define MAX_DATA_BUF 1024
char serial_read_buf[MAX_DATA_BUF];
char send_buf[MAX_DATA_BUF];
#define RCM_FILE_NAME1 "RCM_compressed_data.txt"
#define RCM_FILE_NAME2 "RCM_not_compress_data.txt"
#define RCM_FILE_NAME3 "RCM_origin_important.txt"
int main(int argc, char *argv[])
{
    int len;
    int ret;
    int send_buf_len;
    CTD_S ctd_data;

    if (!parse_arguments(argc, argv)) {
        return EXIT_FAILURE;
    }

    pr_info("%s demo\n", argv[0]);

    /** Serial initialize */
    if ((fd_serial = Serial_open(port, baud, 8, 'n', 1)) < 0) { 
        pr_error("Open serial %s error!\n", port);
//        return -MENOENT;
    }  
    pr_detail("Serial %s opened\n", port);

    /** Send */
    memset(send_buf, 0, sizeof(send_buf));
    strcpy(send_buf, "Hello world!\n");
    send_buf_len = strlen(send_buf);
    ret = Serial_write(fd_serial, send_buf, send_buf_len);
    if (ret)
        pr_detail("Serial Write Success\n");

#if 0
    /** Recv */
    memset(serial_read_buf, 0, sizeof(serial_read_buf));
    while (1) {
        if ((len = Serial_read(fd_serial, serial_read_buf, MAX_DATA_BUF - 2, 10*1000)) <= 0)
            usleep(300 * 1000); /* 300ms */
        else {
            pr_info("%s\n", serial_read_buf);
            break;
        }
    }
#endif
    /** Ctd parser */
    memset(serial_read_buf, 0, sizeof(serial_read_buf));
//    strcpy(serial_read_buf, CTD_STRING1);
    strcpy(serial_read_buf, CTD_STRING2);
    string_token(serial_read_buf, strlen(serial_read_buf), ',',
                 send_buf, sizeof(send_buf));

    //modem_send(serial_read_buf);

    /** RCM parser */
int flen;
FILE *fp_r;
char *rbuf;
int i;
    fp_r = fopen(RCM_FILE_NAME1, "r");
    if (!fp_r)
        pr_error("fopen read file fail!\n");

    fseek(fp_r, 0L, SEEK_END); /* 定位到文件末尾 */
    flen = ftell(fp_r); /* 得到文件大小 */
    rbuf = (char *)malloc(flen + 2); /* 根据文件大小动态分配内存空间 */
    if(NULL == rbuf) {
        fclose(fp_r);
        pr_error("malloc fail!\n");
        return -MENOMEM;
    }
    fseek(fp_r, 0L, SEEK_SET); /* 定位到文件开头 */
    fread(rbuf, flen, 1, fp_r); /* 一次性读取全部文件内容 */
    rbuf[flen] = 0; /* 字符串结束标志 */

    for (i = 0; i < flen; i++)
        pr_detail("%c", rbuf[i]);
     pr_detail("\n");

    free(rbuf);
    fclose(fp_r);

    /** ezxml */
    ezxml_t f1 = ezxml_parse_file(RCM_FILE_NAME1);
    char *s;
    printf("%s\n", (s = ezxml_toxml(f1)));
    free(s);

    ezxml_t Data, SensorData, Parameters, Point;
    const char *sensorname;

    for (Data = ezxml_child(f1, "Data"); Data; Data = Data->next) {
        for (SensorData = ezxml_child(Data, "SensorData"); SensorData; SensorData = SensorData->next) {
        sensorname = ezxml_attr(SensorData, "ID");
        pr_detail("Sensor ID: %s\n", sensorname);
        pr_detail("\tPoint_ID\tValue\n");
            for (Parameters = ezxml_child(SensorData, "Parameters"); Parameters; Parameters = Parameters->next) {
                for (Point = ezxml_child(Parameters, "Point"); Point; Point = Point->next) {
                    printf("\t%s\t\t%s\n", ezxml_attr(Point, "ID"),
                                         ezxml_child(Point, "Value")->txt);
                }
            }
        }
    }

    ezxml_free(f1);

    Serial_close(fd_serial);

    return EXIT_SUCCESS;
}
